package com.joehaivo.hweather.Presenter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.joehaivo.hweather.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by haivo on 2017-10-14.
 */

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder>{
    private Context mContext;
    private List<Map<String,Object>> mData;

    public RecyclerAdapter(Context mContext){
        this.mContext = mContext;
        mData = new ArrayList<>();
    }

    public void initData(List<Map<String,Object>> data){
        mData = data;
        notifyDataSetChanged();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View contentView = LayoutInflater.from(mContext).inflate(R.layout.forecast_recycler_item,parent,false);
        MyViewHolder viewHolder = new MyViewHolder(contentView);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.textDate.setText(mData.get(position).get("DATE").toString());
        holder.imageCondition.setImageDrawable((Drawable) mData.get(position).get("IMAGE_DRAW"));
        holder.textCondition.setText(mData.get(position).get("FORE_CONDITION").toString());
        holder.textHighTmp.setText(mData.get(position).get("HIGH_TMP").toString());
        holder.textLowTmp.setText(mData.get(position).get("LOW_TMP").toString());
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder{
        TextView textDate;
        ImageView imageCondition;
        TextView textCondition;
        TextView textHighTmp;
        TextView textLowTmp;
        MyViewHolder(View itemView) {
            super(itemView);
            textDate = (TextView) itemView.findViewById(R.id.textdate);
            imageCondition = (ImageView) itemView.findViewById(R.id.imageCondition);
            textCondition = (TextView)itemView.findViewById(R.id.textcondition);
            textHighTmp = (TextView)itemView.findViewById(R.id.textHighTmp);
            textLowTmp = (TextView)itemView.findViewById(R.id.textLowTmp);
        }
    }
}

